------------------------------------------------------------------------------------------------------------------------------------------------
--[[

之后进入【彼岸蝶舞】状态（并记录下持续时间t，记录上限为49秒），
每2.8秒流失7点生命（能烧到死，不会被减伤），自身中等范围发光且不能睡觉。

在【彼岸蝶舞】状态下，近战武器每次攻击附加基础值为0.5*H的引魂火伤害。

长柄突刺对每个命中单位附加基础值为H的引魂火伤害，并回复胡桃7点理智。

]]--
------------------------------------------------------------------------------------------------------------------------------------------------
--- PARAM
    local TIME_UPDATE_DELTA = 0.1
------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function is_spell_hidden_weapon(weapon)       --- 隐藏武器造成的伤害
        if weapon and weapon:HasTag("hutao_spell_hidden_weapon") then
            return true
        end
        return false
    end
    local function is_spell_polearm_spike_casting(weapon) --- 长柄 突刺施法期间
        if weapon and weapon:HasTag("hutao_tag.polearm_spike_casting") then
            return true
        end
        return false
    end
    local function target_got_any_damage(damage,spdamage)
        if damage > 0 then
            return true
        end
        for k, v in pairs(spdamage) do
            if v > 0 then
                return true
            end
        end
        return false
    end
------------------------------------------------------------------------------------------------------------------------------------------------
---  sfx
    local function SpawnFx(player)
        -- SpawnPrefab("hutao_sfx_butterflies_and_flames").Transform:SetPosition(player.Transform:GetWorldPosition())
        player:SpawnChild("hutao_sfx_butterflies_and_flames")
    end
------------------------------------------------------------------------------------------------------------------------------------------------
--- 
    local function OnAttached(inst,player)
        inst.entity:SetParent(player.entity)
        inst.Transform:SetPosition(0,0,0)
        -----------------------------------------------------
        --- 
            SpawnFx(player)
        -----------------------------------------------------
        --- 计时器
            inst:DoPeriodicTask(TIME_UPDATE_DELTA,function()            
                if player.components.hutao_com_butterfly_s_embrace:RemainTimeDoDelta(-TIME_UPDATE_DELTA) <= 0 then
                    player.components.hutao_com_butterfly_s_embrace:Stop()
                    print("彼岸蝶舞 结束")
                else
                    local time = player.components.hutao_com_butterfly_s_embrace:ActiveTimeDoDelta(TIME_UPDATE_DELTA)
                    -- print("彼岸蝶舞 持续",time)
                    if math.floor(time*10) % 3.5 == 0 then
                        SpawnFx(player)
                    end
                end
            end)
        -----------------------------------------------------
        --- 定期扣血
            inst:DoPeriodicTask(2.8,function()
                player.components.health:DoDelta(-7,nil,inst.prefab)
            end)
        -----------------------------------------------------
        --- 攻击目标
            inst:ListenForEvent("onhitother",function(_,_table)
                local target = _table and _table.target
                local weapon = _table and _table.weapon
                local damage = _table and _table.damage
                local spdamage = _table and _table.spdamage or {}
                if target and target:IsValid() and target.components.health and not target.components.health:IsDead() then
                    local health_cost = player.components.hutao_com_butterfly_s_embrace:GetHealthCost()
                    -- print("彼岸蝶舞 对目标附加引魂火伤害",health_cost,is_spell_polearm_spike_casting(weapon),not is_spell_hidden_weapon(weapon))
                    if is_spell_polearm_spike_casting(weapon) then
                        SpawnPrefab("hutao_sfx_pink_flame"):PushEvent("Set",{target = target})
                        player.components.hutao_com_fire_damage:DoFireDamage(target,health_cost)
                        player.components.sanity:DoDelta(7,true)
                        return
                    end
                    if not is_spell_hidden_weapon(weapon) then
                        SpawnPrefab("hutao_sfx_pink_flame"):PushEvent("Set",{target = target})
                        player.components.hutao_com_fire_damage:DoFireDamage(target,0.5*health_cost)

                    end
                end
            end,player)
        -----------------------------------------------------
        ---
            player.components.hutao_com_near_danger:AddDanger("hutao_debuff_phantom_butterfly_dance")
            inst:ListenForEvent("onremove",function()
                player.components.hutao_com_near_danger:RemoveDanger("hutao_debuff_phantom_butterfly_dance")                
            end)
        -----------------------------------------------------
    end
------------------------------------------------------------------------------------------------------------------------------------------------


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()
    inst.entity:AddTransform()

    inst:AddTag("CLASSIFIED")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)

    return inst
end

return Prefab("hutao_debuff_phantom_butterfly_dance", fn)
